package com.hwadee.job.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hwadee.job.entity.Company;
import com.hwadee.job.entity.StudentInfo;
import com.hwadee.job.entity.User;
import com.hwadee.job.service.UserService;

@RequestMapping("/user")
@Controller
public class UserLoginController {

	@Autowired
	private UserService userService;

	@GetMapping("/index")
	public String index() {
		return "student/index";
	}
	@GetMapping("/login")
	public String login() {
		return "login/login";
	}
	@GetMapping("/reg")
	public String company() {
		return "login/cregister1";
	}
	@GetMapping("/stu")
	public String student() {
		return "login/sturegister";
	}

	@PostMapping("/userlogin")
	public String log(User user, Model model, HttpSession session,HttpServletRequest request) {
		String username = user.getuName();
		String userpassword = user.getuPassword();
		String usertype = user.getuType();
		if (username == "" || username.equals(null) || userpassword == "" || userpassword.equals(null)) {
			model.addAttribute("msg", "账号或密码不能为空！");
			return "login/login";
		}
		User use = userService.selectByName(username);
		
		if (use == null) {
			model.addAttribute("msg", "账号或密码错误！");
			return "login/login";
		}
		if(!usertype.equals(use.getuType())) {
			model.addAttribute("msg", "用户身份不对！");
			return "login/login";
		}
		
		
		if (userpassword.equals(use.getuPassword()) && use.getuType().equals("student")) {
			session.setAttribute("user", use);
			return "redirect:/user/index";
		}else if(userpassword.equals(use.getuPassword()) && use.getuType().equals("company")) {
			session.setAttribute("user", use);
			return "redirect:/recruit/crecruit1";
		}else {
			model.addAttribute("msg", "账号或密码错误！");
			return "login/login";
		}
	}
	
	@GetMapping("/registered")
	public String registered() {
//		return "login/cregister1";
		return "login/register";
	}
	
	@PostMapping("/cregister")
	public String companyregister(User user, HttpSession session, Model model) {
		String usertype = user.getuType();
		String username = user.getuName();
		User use = userService.selectByName(username);
		
		if (use==null || use.equals(" ") && usertype.equals("student")) {
			
			userService.insert(user);
			return "redirect:/user/stu";
		}else if(use==null || use.equals(" ") && usertype.equals("company")) {
			
			userService.insert(user);
			return "redirect:/user/reg";
		}else {
			model.addAttribute("msg", "用户已存在！");
			return "login/register";
		}
	}
	
	@PostMapping("/comregistered")
	public String reg(Company company) {
		userService.insertSelective(company);
		
		return "redirect:/user/login";
	}
	
	@PostMapping("/sturegistered")
	public String student(StudentInfo student) {
		userService.insertStudent(student);
		
		return "redirect:/user/login";
	}

}
